package httpserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import pojo.OnlineSocket;
import centerserver.Server;
import util.GenerateHTML;

public class HttpServer {
	
	private static final int HTTP_SERVER_PORT = 8007;
	private static final int CENTER_SERVER_PORT = 8081;
	
	private long startTime = 0;
	private long endTime = 0;
	private boolean isOpenServer;
	private Map<String, OnlineSocket> onlineUsers;
	private Server centerServer;
	
	public void service() throws IOException{
		ServerSocket server = new ServerSocket(HTTP_SERVER_PORT);
		Matcher matcher = null;
		String regex = "^GET\\s+(.+)\\s+HTTP/1\\.1$";
		
		while (true) {
			Socket socket = server.accept();
			InputStream in = socket.getInputStream();
			OutputStream out = socket.getOutputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			String line;
			String url = null;
			
			while (!(line = reader.readLine()).trim().isEmpty()) {
				
				matcher = Pattern.compile(regex).matcher(line);
				if (matcher.find()) {
					url = matcher.group(1);
				}
				
//				if (line.contains("GET / HTTP/1.1")) {
//					url = "/";
//				} else if (line.contains("GET /start HTTP/1.1")) {
//					url = "/start";
//				} else if (line.contains("GET /end HTTP/1.1")) {
//					url = "/end";
//				} 
			}
			System.out.println("URL:" + url);
			
			if (url == null) {
				System.out.println("url is null");
				String nok = "HTTP/1.1 404 Not Found\r\n\r\n";
				out.write(nok.getBytes());
				continue;
			}
			String ok = "HTTP/1.1 200 OK\r\n\r\n";
			switch (url) {
			case "/":
				out.write(ok.getBytes());
				
				// generate HTML file to return
				Element root = GenerateHTML.generate(onlineUsers, startTime,isOpenServer);
				
				XMLOutputter outputter = new XMLOutputter();
				outputter.output(root, out);
				break;
			case "/start":
				System.out.println("Server Started.");
				
				/** start server here **/
				centerServer = new Server(new ServerSocket(CENTER_SERVER_PORT));
				isOpenServer = true;
				this.onlineUsers = centerServer.getOnlineUser();
				new Thread(centerServer).start();
				startTime = System.currentTimeMillis();

				out.write(ok.getBytes());

				Element root2 = GenerateHTML.genResponse();

				XMLOutputter outputter2 = new XMLOutputter();
				outputter2.output(root2, out);
				break;
			case "/end":
				if (centerServer != null) {
					/** end server here **/
					centerServer.close();
					isOpenServer = false;
					endTime = System.currentTimeMillis();
				}
				
				out.write(ok.getBytes());

				Element root1 = GenerateHTML.genResponse();

				XMLOutputter outputter1 = new XMLOutputter();
				outputter1.output(root1, out);
				break;
			default:
				break;
			}
			
			out.close();
			reader.close();
			socket.close();
		}
	}
	
}
